/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIMantenimientoParametrosSeguridad.java
 *
 * Created on Oct 19, 2011, 6:42:24 PM
 */
package GUI.Seguridad;

import BE.BEParametrosSeguridad;
import BL.BLParametrosSeguridad;
import BL.BLValidaciones;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;

/**
 *
 * @author hunter2409
 */
public class GUIMantenimientoParametrosSeguridad extends javax.swing.JDialog {
    final static String badchars = "`~!@#$%^&*()_+=\\|\"':;?/>.<, ";
    BEParametrosSeguridad objParametros;
    BLParametrosSeguridad objGestorParametros;
    BLValidaciones objValidaciones;

    /** Creates new form GUIMantenimientoParametrosSeguridad */
    public GUIMantenimientoParametrosSeguridad(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        objGestorParametros= new BLParametrosSeguridad();
        objParametros =  new BEParametrosSeguridad();
        objValidaciones= new BLValidaciones();
        initComponents();
        txtLongMinima.addKeyListener(objValidaciones.soloNumeros);
        txtNCharNoNum.addKeyListener(objValidaciones.soloNumeros);
        txtNCharNum.addKeyListener(objValidaciones.soloNumeros);
        txtNContrasenhaHistoricas.addKeyListener(objValidaciones.soloNumeros);
        txtNIntFallidos.addKeyListener(objValidaciones.soloNumeros);
        txtTiempoRotacion.addKeyListener(objValidaciones.soloNumeros);
        txtTiempoSesion.addKeyListener(objValidaciones.soloNumeros);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        txtTiempoRotacion = new javax.swing.JTextField();
        txtNIntFallidos = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTiempoSesion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNContrasenhaHistoricas = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtLongMinima = new javax.swing.JTextField();
        txtNCharNoNum = new javax.swing.JTextField();
        txtNCharNum = new javax.swing.JTextField();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administrar Parametros de Seguridad");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Politicas de Seguridad"));

        txtTiempoRotacion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTiempoRotacionKeyTyped(evt);
            }
        });

        txtNIntFallidos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNIntFallidosKeyTyped(evt);
            }
        });

        jLabel4.setText("Intentos Fallidos:");

        jLabel5.setText("Tiempo de Rotación (dias) :");

        jLabel6.setText("Tiempo de Sesion:");

        txtTiempoSesion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtTiempoSesionKeyTyped(evt);
            }
        });

        jLabel7.setText("Numero de Contraseñas historicas:");

        txtNContrasenhaHistoricas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNContrasenhaHistoricasKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtNContrasenhaHistoricas)
                    .addComponent(txtTiempoRotacion)
                    .addComponent(txtNIntFallidos, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .addComponent(txtTiempoSesion, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE))
                .addContainerGap(75, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNIntFallidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtTiempoRotacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTiempoSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtNContrasenhaHistoricas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Caracteristicas de la Contraseña"));

        jLabel1.setText("Longitud Minima:");

        jLabel2.setText("Caracteres No Numericos:");

        jLabel3.setText("Caracteres Numericos:");

        txtLongMinima.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtLongMinimaKeyTyped(evt);
            }
        });

        txtNCharNoNum.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNCharNoNumKeyPressed(evt);
            }
        });

        txtNCharNum.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNCharNumKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(53, 53, 53)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtNCharNoNum, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
                    .addComponent(txtLongMinima)
                    .addComponent(txtNCharNum, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtLongMinima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNCharNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNCharNoNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        btnAceptar.setText("Aceptar");
        btnAceptar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtNContrasenhaHistoricasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNContrasenhaHistoricasKeyTyped
    if(txtNContrasenhaHistoricas.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtNContrasenhaHistoricasKeyTyped

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    objGestorParametros.cargar();
    objParametros=objGestorParametros.getObjParametros();
    
    txtLongMinima.setText(objParametros.getLongMinima()+"");
    txtNCharNoNum.setText(objParametros.getnCaractNoNum()+"");
    txtNCharNum.setText(objParametros.getnCaractNum()+"");
    txtNContrasenhaHistoricas.setText(objParametros.getnContrasenhaHistoricas()+"");
    txtNIntFallidos.setText(objParametros.getnIntentosFallidos()+"");
    txtTiempoRotacion.setText(objParametros.getTiempoRotacion()+"");
    txtTiempoSesion.setText(objParametros.getTiempoSesion()+"");
}//GEN-LAST:event_formWindowOpened

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
     int result=JOptionPane.showConfirmDialog(null,"Desea guardar los parametros de Seguridad?", "Registro del Parametros de Seguridad",    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            //JOptionPane.showMessageDialog(null,"Problema al consultar la base de datos"+e.toString(),"Error",JOptionPane.ERROR_MESSAGE);

    if (result==JOptionPane.YES_OPTION)
    {
        objParametros = new BEParametrosSeguridad();
        objParametros.setLongMinima(Integer.parseInt(txtLongMinima.getText()));
        objParametros.setTiempoRotacion(Integer.parseInt(txtTiempoRotacion.getText()));
        objParametros.setTiempoSesion(Integer.parseInt(txtTiempoSesion.getText()));
        objParametros.setnCaractNoNum(Integer.parseInt(txtNCharNoNum.getText()));
        objParametros.setnCaractNum(Integer.parseInt(txtNCharNum.getText()));
        objParametros.setnContrasenhaHistoricas(Integer.parseInt(txtNContrasenhaHistoricas.getText()));
        objParametros.setnIntentosFallidos(Integer.parseInt(txtNIntFallidos.getText()));
        
        String resultado=objGestorParametros.actualizar(objParametros);
        
        JOptionPane.showMessageDialog(null,resultado,"Registro de Parametros de Seguridad",JOptionPane.INFORMATION_MESSAGE);
        this.dispose();
    }
}//GEN-LAST:event_btnAceptarActionPerformed

private void txtLongMinimaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLongMinimaKeyTyped
    if(txtLongMinima.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtLongMinimaKeyTyped

private void txtNCharNumKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNCharNumKeyTyped
    if(txtNCharNoNum.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtNCharNumKeyTyped

private void txtNCharNoNumKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNCharNoNumKeyPressed
    if(txtNCharNoNum.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtNCharNoNumKeyPressed

private void txtNIntFallidosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNIntFallidosKeyTyped
    if(txtNIntFallidos.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtNIntFallidosKeyTyped

private void txtTiempoRotacionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTiempoRotacionKeyTyped
    if(txtTiempoRotacion.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtTiempoRotacionKeyTyped

private void txtTiempoSesionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtTiempoSesionKeyTyped
    if(txtTiempoSesion.getText().length()>2) evt.consume();
}//GEN-LAST:event_txtTiempoSesionKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIMantenimientoParametrosSeguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIMantenimientoParametrosSeguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIMantenimientoParametrosSeguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIMantenimientoParametrosSeguridad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUIMantenimientoParametrosSeguridad dialog = new GUIMantenimientoParametrosSeguridad(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtLongMinima;
    private javax.swing.JTextField txtNCharNoNum;
    private javax.swing.JTextField txtNCharNum;
    private javax.swing.JTextField txtNContrasenhaHistoricas;
    private javax.swing.JTextField txtNIntFallidos;
    private javax.swing.JTextField txtTiempoRotacion;
    private javax.swing.JTextField txtTiempoSesion;
    // End of variables declaration//GEN-END:variables
}
